package cn.kevin.puzzle;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.loader.ImageLoader;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

public class PuzzleActivity extends AppCompatActivity implements View.OnTouchListener {
    private static final int IMAGE_PICKER = 100;
    PuzzleLayout puzzleLayout;
    TextView tvTips;
    ImageView ivTips;
    int drawableId = R.mipmap.pic_02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_puzzle);
        ivTips = (ImageView) findViewById(R.id.iv_tips);
        ivTips.setImageResource(drawableId);
        tvTips = (TextView) findViewById(R.id.tv_tips);
        tvTips.setOnTouchListener(this);
        puzzleLayout = (PuzzleLayout) findViewById(R.id.activity_swipe_card);
//        puzzleLayout.setImage(drawableId, squareRootNum);

        final TextView tvStep = (TextView) findViewById(R.id.step);

        puzzleLayout.setOnCompleteCallback(new PuzzleLayout.OnCompleteCallback() {
            @Override
            public void onComplete() {
                new AlertDialog.Builder(PuzzleActivity.this)
                        .setTitle("恭喜")
                        .setMessage("您已完成拼图")
                        .show();
            }

            @Override
            public void onStep(int step) {
                tvStep.setText(String.format(Locale.getDefault(), "步数：%d", step));
            }
        });

        findViewById(R.id.pick_photo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagePicker picker = ImagePicker.getInstance();
                picker.setImageLoader(new PickerImageLoader());
                picker.setCrop(false);
                picker.setShowCamera(true);
                Intent intent = new Intent(PuzzleActivity.this, ImageGridActivity.class);
                startActivityForResult(intent, IMAGE_PICKER);
            }
        });
    }

    private void setImage(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        int checkedId = radioGroup.getCheckedRadioButtonId();
        int column = 2;
        if (checkedId == R.id.three) {
            column = 3;
        }
        if (checkedId == R.id.four) {
            column = 4;
        }
        puzzleLayout.setImage(path, column);
        ivTips.setImageURI(Uri.parse(path));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == IMAGE_PICKER) {
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null && images.size() > 0) {
                    setImage(images.get(0).path);
                }
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private static class PickerImageLoader implements ImageLoader {

        @Override
        public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
            Picasso.get()
                    .load(Uri.fromFile(new File(path)))//
                    .resize(width, height)//
                    .centerInside()//
                    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)//
                    .into(imageView);
        }

        @Override
        public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {

        }

        @Override
        public void clearMemoryCache() {

        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ivTips.setVisibility(View.VISIBLE);
                break;
            default:
                ivTips.setVisibility(View.GONE);
        }
        return true;
    }
}
